package com.funtl.framework.web.service.purview.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.funtl.framework.java.common.util.bean.BeanUtil;
import com.funtl.framework.java.exception.DaoException;
import com.funtl.framework.java.exception.ServiceException;
import com.funtl.framework.java.orm.hibernate.service.HibernateParam;
import com.funtl.framework.java.orm.hibernate.service.IHibernateDao;
import com.funtl.framework.web.entity.PurviewMenu;
import com.funtl.framework.web.service.purview.IPurviewMenuService;

@Component
public class PurviewMenuServiceImpl implements IPurviewMenuService {
	private IHibernateDao hibernateDao;
	
	//service
	
	@Override
	public List<String> queryActionMenu(String managerId) throws ServiceException {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT menu_url FROM t_purview_menu pa ");
		sql.append("INNER JOIN t_purview_group pg ON pg.purview_menu_id = pa.id ");
		sql.append("WHERE pg.purview_manager_id = ?");
		List<Map<String, Object>> list = hibernateDao.getListBySQL(sql.toString(), new String[] {managerId});
		
		List<String> actionMethods = null;
		if (list != null && list.size() > 0) {
			actionMethods = new ArrayList<String>();
			for (Map<String, Object> map : list) {
				actionMethods.add(String.valueOf(map.get("menu_url")));
			}
		}
		return actionMethods;
	}

	//common service
	
	@Override
	public PurviewMenu get(PurviewMenu entity) throws ServiceException {
		try {
			HibernateParam param = new HibernateParam();
			param.setPropertyTypes(new String[] {"id"});
			param.setOperators(new String[] {"="});
			param.setPropertyValues(new String[] {entity.getId()});
			return hibernateDao.get(entity, param);
		} catch (DaoException e) {
			throw new ServiceException("get", e);
		}
	}

	@Override
	public Long getCount(PurviewMenu entity) throws ServiceException {
		return null;
	}

	@Override
	public List<PurviewMenu> getList(PurviewMenu entity) throws ServiceException {
		try {
			return hibernateDao.getListByEhcatch(entity, "menuNo", "ASC");
		} catch (DaoException e) {
			throw new ServiceException("getList", e);
		}
	}

	@Override
	public String insert(PurviewMenu entity) throws ServiceException {
		try {
			hibernateDao.insert(entity);
			return null;
		} catch (DaoException e) {
			throw new ServiceException("insert", e);
		}
	}

	@Override
	public String delete(PurviewMenu entity) throws ServiceException {
		try {
			hibernateDao.delete(entity, "id");
			return null;
		} catch (DaoException e) {
			throw new ServiceException("delete", e);
		}
	}

	@Override
	public String update(PurviewMenu entity) throws ServiceException {
		try {
			PurviewMenu purviewMenu = get(entity);
			BeanUtil.beanToBean(entity, purviewMenu);
			hibernateDao.update(entity);
			return null;
		} catch (DaoException e) {
			throw new ServiceException("update", e);
		}
	}

	@Override
	public List<PurviewMenu> page(PurviewMenu entity) throws ServiceException {
		return null;
	}
	
	//getter setter
	
	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	@Resource
	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

}
